<template>
  <div class="login-container">
    <div class="login-background"></div>

    <div class="login-card">
      <div class="login-header">
        <h1>劳动实践活动服务系统</h1>
        <p>请选择您的身份并登录</p>
      </div>

      <UserTypeCard
          v-model="userType"
          class="user-type-selector"
      />

      <LoginForm
          v-if="userType"
          :user-type="userType"
          @login-success="handleLoginSuccess"
      />
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import { useUserStore } from '@/store/modules/user';
import UserTypeCard from './UserTypeCard.vue';
import LoginForm from './LoginForm.vue';
import { resetActiveRoutes } from '@/router'; // 导入路由添加函数

const route = useRoute();
const router = useRouter();
const userStore = useUserStore();
const userType = ref<'student' | 'teacher' | 'admin'>();

const handleLoginSuccess = () => {
  // 重置路由
  resetActiveRoutes();

  // 获取重定向路径（简单处理，直接取参数）
  const redirectPath = route.query.redirect as string || `/${userStore.userType}/dashboard`;

  // 直接跳转（hash模式需要手动加#）
  window.location.hash = redirectPath.startsWith('#')
      ? redirectPath
      : `#${redirectPath}`;
};


// 验证路径是否有效
const isValidPath = (path: string): boolean => {
  const validPrefixes = ['/student', '/teacher', '/admin'];
  return validPrefixes.some(prefix => path.startsWith(prefix));
};

// 获取默认重定向路径
const getDefaultRedirectPath = (): string => {
  switch (userType.value) {
    case 'student': return '/student/dashboard';
    case 'teacher': return '/teacher/dashboard';
    case 'admin': return '/admin/dashboard';
    default: return '/';
  }
};
</script>

<style scoped>
/* 原有样式保持不变 */
.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background-color: #f5f7fa;
  position: relative;
}

.login-background {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: linear-gradient(120deg, #3494e6, #ec6ead);
  opacity: 0.8;
  z-index: 0;
}

.login-card {
  width: 100%;
  max-width: 450px;
  background: white;
  border-radius: 12px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
  padding: 40px;
  z-index: 1;
  position: relative;
}

.login-header {
  text-align: center;
  margin-bottom: 30px;
}

.login-header h1 {
  font-size: 28px;
  color: #2c3e50;
  margin-bottom: 10px;
}

.login-header p {
  color: #7f8c8d;
  font-size: 16px;
}

.user-type-selector {
  margin-bottom: 30px;
}
</style>
